﻿var express = require('express');
var app = express();
var http = require('http').Server(app);
var httpget = require('http');
var bodyParser = require('body-parser');
//var cookieParser = require('cookie-parser');
var session = require('express-session');

//var sessioncookie = require('./sessioncookie.js')
//str=e80b5017098950fc58aad83c8c14978e  默认32位小写 
//以下为ajax调用使用
//function func() {
//    $.ajax({
//        type: "GET",
//        dataType: "json",
//        xhrFields: {
//            withCredentials: true    // 要在这里设置  
//        },
//        url: 'https://xxx.com/api/login',
//        success: function (data) {

//        },
//        beforeSend: function (xhr) {
//            //                下面的设置无效  
//            //                xhr.withCredentials = true;  
//        },
//        error: function (err) {
//            alert(JSON.stringify(err))
//        }
//    })
//}
//responce.set('Access-Control-Allow-Origin', request.get('origin'));
//在线
//在线
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json());
app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", req.headers.origin); //需要显示设置来源
    //res.header("Access-Control-Allow-Origin", "http://192.168.1.237:8001");
    res.header("Access-Control-Allow-Credentials", true);
    res.header("Access-Control-Allow-Headers", "X-Requested-With,Content-Type");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");

    res.header("X-Powered-By", 'BIM');
    // res.header("Content-Type", "application/json;charset=utf-8");
    next();
});
//app.use(cookieParser('secret'));
app.use(session({
    secret: 'secret',
    resave: true,
    saveUninitialized: false,
    cookie: {
        maxAge: 1000 * 60 * 10 //过期时间设置(单位毫秒)
        //,secure:true
    }
}));


//
app.use(express.static(__dirname));
http.listen(80, function () {
    console.log('listening on *:80,good');
});


//用户登陆
app.post('/userlogin', function (req, res) {
    //console.log(req.body);
    //var id = req.body.id;
    //var pass = req.body.pass;
    //req.session.user = id;
   // res.setHeader('Access-Control-Allow-Origin', req.headers.origin);
    //res.setHeader('Access-Control-Allow-Credentials', true);
    req.session.user = req.body.id;
    console.log(req.session.id,req.session.user);
    //var writehead = res.writehead;
    //res.writehead = function () {
    //    var cookies = res.getHeader('Set-Cookie');
    //    var session = serialize(key, req.session.id);
    //    cookies = Array.isArray(cookies) ? cookies.concat(session) : [cookies, session];
    //    res.setHeader('Set-Cookie', cookies);
    //    return writehead.apply(this, arguments);
    //}

    //sessioncookie.do1(req, res);
    res.send({id:req.session.id});

   // res.send({ ret: 1 ,info:"ok"});
});
app.get('/logout', function (req, res) {
    req.session.id = null;
    req.session.pass = null;
    res.redirect('/admin/login.html');
});
app.get('/user', function (req, res) {
   // var id = req.params.id;
   // console.log(id);
    console.log(req.session.user, req.session.id);
    res.send({ info: req.session.user });
});